<?php
include_once APPLICATION_DIR . "/application/model/BUSINESS_SOLUTION.php";

class Business_Solution extends DataObjects_BUSINESS_SOLUTION
{
	public function validateInput(){
        global $error_msg;
		
		$posted_file = $_FILES["IMAGE"];
		if ($error_msg->isNoError() && !empty($posted_file["name"])) {
			if ($this->ID) {
				$this->OLD_IMAGE = $this->IMAGE;
			}
            $upload = new UPLOAD();
			$new_file = $upload->doUploadImage(IMAGE_DIR."/", $posted_file, '', '190', '');
			if ($new_file) {
				$this->IMAGE = $new_file;
			} else {
				$error_msg->add('_MSG_UPLOAD_FAILED');
			}
		}

        return $error_msg->isNoError();
    }
	
	public function update() {
		if (!empty($this->OLD_IMAGE) && $this->IMAGE != $this->OLD_IMAGE) {
			@unlink(IMAGE_DIR."/{$this->OLD_IMAGE}");
			@unlink(IMAGE_DIR."/thumb{$this->OLD_IMAGE}");
		}

		return parent::update();
	}

	public function delete() {
		if ($this->IMAGE) {
			@unlink(IMAGE_DIR."/{$this->IMAGE}");
			@unlink(IMAGE_DIR."/thumb{$this->IMAGE}");
		}

		return parent::delete();
	}

	public static function getDir()
	{
		return IMAGE_DIR;
	}

	public function getPath()
	{
		return self::getDir()."/{$this->IMAGE}";
	}

    public static function deletable($id){
		// check if exists a child node?
		$bs = new Business_Solution();
		$bs->ID_PARENT = $id;
		if($bs->count()>0){
			return false;
		}
		return true;
	}
    public static function getTree(&$tree = array(),$id_parent=0){
        $bs = new Business_Solution();
		$bs->ID_PARENT = $id_parent;
		$bs->orderBy('POS');
		$bs->find();
        while($bs->fetch()){
            $tree[$bs->ID]['TITLE'] = $bs->TITLE;
            $tree[$bs->ID]['IMAGE'] = $bs->IMAGE;
            $tree[$bs->ID]['SHORT_DESC'] = $bs->SHORT_DESC;
            $tree[$bs->ID]['subs'] = array();
            $tree[$bs->ID]['deletable'] = Business_Solution::deletable($bs->ID);
            self::getTree($tree[$bs->ID]['subs'],$bs->ID);
        }
    }
    public static function getSubItem($id_parent){
        $bs = new Business_Solution();
        $sql = "SELECT * FROM BUSINESS_SOLUTION WHERE ID_PARENT = {$id_parent} AND DISABLED = 0 ORDER BY POS ASC";
        $bs->query($sql);
        return $bs;
    }
    public static function getAllSubItem($id_parent,&$arrBS,$path){
        $bs = new Business_Solution();
        $bs->ID_PARENT = $id_parent;
        $bs->DISABLED = 0;
        $bs->orderBy("POS ASC");
        $bs->find();
        while($bs->fetch()){
            if(!isset($arrBS[$bs->ID_PARENT])){
                $arrBS[$bs->ID] = array();
            }
            $arrBS[$bs->ID] = array("ID"=> $bs->ID, "TITLE" => $bs->TITLE);
            if(in_array($bs->ID, $path)){
                $arrBS[$bs->ID]["HAVE_SUB"] = true;
                self::getAllSubItem($bs->ID,$arrBS[$bs->ID]["SUB"],$path);
            }
            else{
                $arrBS[$bs->ID]["HAVE_SUB"] = false;
            }
        }
    }
    
    public static function getBusinessSolutionPath($id,&$path){
        $bs = new Business_Solution();
        $bs->ID = $id;
        $bs->DISABLED = 0;
        $bs->orderBy("POS ASC");
        $bs->find(1);
        if($bs->ID_PARENT >0){
            $path[] = $bs->ID_PARENT;
            self::getBusinessSolutionPath($bs->ID_PARENT, $path);
        }       
    }
    public static function getBusinessSolutionPathName($id,&$path){
        $bs = new Business_Solution();
        $bs->ID = $id;
        $bs->DISABLED = 0;
        $bs->orderBy("POS ASC");
        $bs->find(1);
        $path[] = array("ID"=>$bs->ID, "TITLE"=>$bs->TITLE) ;
        if($bs->ID_PARENT >0){
            self::getBusinessSolutionPathName($bs->ID_PARENT, $path);
        }       
    }
}
?>
